package jp.co.technotree.dao.order.bean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import jp.co.technotree.common.dao.BaseDAO;
import jp.co.technotree.common.dao.HashBean;
import jp.co.technotree.common.dao.ReadOnlyRowSet;
import jp.co.technotree.common.dao.StatementValue;
import jp.co.technotree.common.util.DBGLog;
import jp.co.technotree.exception.DataAccessException;

public class OrderStandardDAO extends BaseDAO {
	private static String CLASSNAME=OrderStandardDAO.class.getName();
	
	/**
	 * DN納品書のPDF用にデータ取得
	 * 
	 * @param report_cd レポート番号
	 * @return jasper で流し込むときのフィールドデータ
	 * @throws DataAccessException
	 */
	public Map[] getOrderMap(String report_cd) throws DataAccessException {
		String METHODNAME = "getOrderMap";
		DBGLog.log(CLASSNAME, METHODNAME, "START");

		Map<String, Object>[] orderMap = null;
		String sql = "";
		
		sql += "  SELECT";
		sql += "  	 supplier_cd AS supplier_cd_full";
		sql += "  	,substring(supplier_cd, 1, 4) AS supplier_cd";
		sql += "  	,supplier_name";
		sql += "  	,TO_CHAR(recv_date, 'YYMMDD') AS recv_date";
		sql += "  	,invoice_cd";
		sql += "  	,item_cd";
		sql += "  	,item_name";
		sql += "  	,delivery_cd";
		sql += "  	,delivery_name";
		sql += "  	,order_unit";
		sql += "  	,TO_CHAR(delivery_date, 'YY/MM/DD') as delivery_date";
		sql += "  	,TO_CHAR(delivery_time, 'HH24:MI') as delivery_time";
		sql += "  	,trunc(order_qty) AS order_qty";
		sql += "  	,check_kbn";
		sql += "  	,const_cd";
		sql += "  	,custdeli_no";
		sql += "  	,supply_dest";
		sql += "  	,customer_name";
		sql += "  	,note";
		//sql += "  	,place_cd ~ '^[0-9]{4}' AS is_real_del";
		//sql += "  	,place_cd !~ '^[0-9]{4}' AS is_supply_del";
		sql += "  	,'１営業所' AS recv_place";
		sql += "  FROM";
		sql += "  	hrs_order_tbl";
		sql += "  WHERE";
		sql += "  		order_kbn != '1'";	// 区が1ならPDF出力しない
		sql += "  	AND";
		sql += "  		report_cd = '" + report_cd + "'";

		DBGLog.log(CLASSNAME, METHODNAME, "SQL:" + sql);
		ReadOnlyRowSet rowSet = executeSQL(sql);

		// 特殊なケースは rowSetをそのまま使用する。
		try {
			int rowSize = rowSet.getMaxRows();
			if (0 < rowSize) {
				rowSet.first();
				orderMap = new HashMap[rowSize];

				for (int i = 0; i < rowSize; i++) {
					Map<String, Object> map = new HashMap<String, Object>();

					map.put("supplier_cd_full", rowSet.getString("supplier_cd_full"));
					map.put("supplier_cd", rowSet.getString("supplier_cd"));
					map.put("supplier_name", rowSet.getString("supplier_name"));
					map.put("recv_date", rowSet.getString("recv_date"));
					map.put("invoice_cd", rowSet.getString("invoice_cd"));
					map.put("item_cd", rowSet.getString("item_cd"));
					map.put("item_name", rowSet.getString("item_name"));
					map.put("delivery_cd", rowSet.getString("delivery_cd"));
					map.put("delivery_name", rowSet.getString("delivery_name"));
					map.put("order_unit", rowSet.getString("order_unit"));
					map.put("delivery_date", rowSet.getString("delivery_date"));
					map.put("delivery_time", rowSet.getString("delivery_time"));
					map.put("order_qty", rowSet.getString("order_qty"));
					map.put("check_kbn", rowSet.getString("check_kbn"));
					map.put("const_cd", rowSet.getString("const_cd"));
					map.put("custdeli_no", rowSet.getString("custdeli_no"));
					map.put("supply_dest", rowSet.getString("supply_dest"));
					map.put("customer_name", rowSet.getString("customer_name"));
					map.put("note", rowSet.getString("note"));
//					map.put("is_real_del", ("t".equals(rowSet.getString("is_real_del"))) ? true : false);
//					map.put("is_supply_del", ("t".equals(rowSet.getString("is_supply_del"))) ? true : false);
					map.put("recv_place", rowSet.getString("recv_place"));

					orderMap[i] = map;

					rowSet.next();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		DBGLog.log(CLASSNAME, METHODNAME, "END");
		return orderMap;
	}
	

	/**
	 * 受注データ一覧、親
	 * 
	 * @param idList
	 * @return
	 * @throws DataAccessException
	 */
	public HashBean getOrderParentList() throws DataAccessException {
		String METHODNAME = "getOrderParentList";
		DBGLog.log(CLASSNAME, METHODNAME, "START");
		
		ArrayList<StatementValue> whereArray = new ArrayList<StatementValue>();

		
		String sql = "";
		sql += " select \n";
		sql += " 	id \n";
		sql += " 	,line_cd as record02 \n";
		sql += " from t_plan \n";
		
		// --------------------------------------------------------------
		// 以下HashBean使用時の定型文
		HashBean hashBean = new HashBean();
		try {
			// SQL実行
			DBGLog.log(CLASSNAME,METHODNAME,sql);

			ReadOnlyRowSet rowSet = executeSQL(sql);
			//rowSet = executeSQL(sql,0,0);
			
			// フィールドデータのセット
			hashBean.setFields(rowSet.getColumnNames());
			
			for ( int i=0; i<rowSet.getMaxRows(); i++ ){
				// 次の行へ
				rowSet.next();
				try {
					// 新しいラインの追加
					hashBean.add();
					
					// 全て文字列でセット
					hashBean.setString(rowSet);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// --------------------------------------------------------------

		DBGLog.log(CLASSNAME, METHODNAME, "END");
		return hashBean;
	}
	
	
}
